<template>
  <div class="page-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button
          v-permissions="{ role: ['ROOT'], permission: ['sys:user:add'] }"
          :icon="Plus"
          type="primary"
          @click="handleEdit($event)"
        >
          添加
        </el-button>
        <!--        <el-button :icon="Delete" type="danger" @click="handleDelete($event)">-->
        <!--          批量删除-->
        <!--        </el-button>-->
      </vab-query-form-left-panel>
      <vab-query-form-left-panel :span="15">
        <el-form inline :model="queryForm" @submit.prevent>
          <el-form-item label="平台账号">
            <el-select v-model="queryForm.accountId" clearable>
              <el-option
                v-for="item in accountOperateList"
                :key="item.id"
                :label="item.platformAccount"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button :icon="Search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table v-loading="listLoading" border :data="list">
      <el-table-column
        type="index"
        width="60"
        header-align="center"
        align="center"
        label="序号"
      />
      <el-table-column
        label="平台账号"
        prop="platformAccount"
        show-overflow-tooltip
        header-align="center"
        align="center"
      />
      <el-table-column
        label="运营人员"
        prop="operateName"
        show-overflow-tooltip
        header-align="center"
        align="center"
      />
      <el-table-column
        label="客服人员"
        prop="customerName"
        show-overflow-tooltip
        header-align="center"
        align="center"
      />
      <el-table-column
        label="累计/新增访问数"
        prop="newVisitsNumber"
        show-overflow-tooltip
        header-align="center"
        align="center"
      />
      <el-table-column
        label="观看时长"
        prop="watchTime"
        show-overflow-tooltip
        header-align="center"
        align="center"
      />
      <el-table-column
        label="新增点赞"
        prop="newPraise"
        show-overflow-tooltip
        header-align="center"
        align="center"
      />
      <el-table-column
        label="新增评论"
        prop="newReview"
        show-overflow-tooltip
        header-align="center"
        align="center"
      />
      <el-table-column
        label="新增笔记分享"
        prop="newNote"
        show-overflow-tooltip
        header-align="center"
        align="center"
      />
      <el-table-column
        label="笔记涨粉"
        prop="chalkUp"
        show-overflow-tooltip
        header-align="center"
        align="center"
      />
      <el-table-column
        label="薯条消耗"
        prop="frenchFryConsumption"
        show-overflow-tooltip
        header-align="center"
        align="center"
      />
      <el-table-column
        label="主页访问"
        prop="homeVisitNum"
        show-overflow-tooltip
        header-align="center"
        align="center"
      />
      <el-table-column
        label="私信量"
        prop="communicationNum"
        show-overflow-tooltip
        header-align="center"
      />
      <el-table-column
        label="有效客资"
        prop="canGuestCapital"
        show-overflow-tooltip
        header-align="center"
        align="center"
      />
      <el-table-column
        label="新客成单数"
        prop="newSuccessfulNum"
        show-overflow-tooltip
        header-align="center"
        align="center"
      />
      <el-table-column
        label="新客成交额"
        prop="newSuccessfulPrice"
        show-overflow-tooltip
        header-align="center"
        align="center"
      />
      <el-table-column
        label="老客成单数"
        prop="oldSuccessfulNum"
        show-overflow-tooltip
        header-align="center"
        align="center"
      />
      <el-table-column
        label="老客成交额"
        prop="oldSuccessfulPrice"
        show-overflow-tooltip
        header-align="center"
        align="center"
      />
      <el-table-column
        label="总成交单数"
        prop="successfulSum"
        show-overflow-tooltip
        header-align="center"
        align="center"
      />
      <el-table-column
        label="成交总额"
        prop="successfulSumPrice"
        show-overflow-tooltip
        header-align="center"
        align="center"
      />
      <el-table-column
        label="有效客资数"
        prop="canGuestCapitalRate"
        show-overflow-tooltip
        header-align="center"
        align="center"
      />
      <el-table-column
        label="客资成本"
        prop="costGuestCapital"
        show-overflow-tooltip
        header-align="center"
        align="center"
      />
      <el-table-column
        label="成交率"
        prop="transactionRate"
        show-overflow-tooltip
        header-align="center"
        align="center"
      />
      <el-table-column
        label="ROI"
        prop="hroi"
        show-overflow-tooltip
        header-align="center"
        align="center"
      />
      <el-table-column
        label="创建时间"
        prop="createTime"
        show-overflow-tooltip
        header-align="center"
        align="center"
      />
      <el-table-column
        label="操作"
        width="200"
        header-align="center"
        align="center"
        fixed="right"
      >
        <template #default="{ row }">
          <!--          <el-button text type="primary" @click="handleEdit(row)">-->
          <!--            编辑-->
          <!--          </el-button>-->
          <!--          <el-button text type="primary" @click="handleDetail(row)">-->
          <!--            汇报-->
          <!--          </el-button>-->
          <!--          <el-button text type="primary" @click="handleDelete(row)">-->
          <!--            删除-->
          <!--          </el-button>-->
          <el-tooltip
            class="box-item"
            effect="dark"
            content="编辑"
            placement="top"
          >
            <el-button :icon="Edit" type="warning" @click="handleEdit(row)" />
          </el-tooltip>
          <el-tooltip
            class="box-item"
            effect="dark"
            content="删除"
            placement="top"
          >
            <el-button
              :icon="Delete"
              type="danger"
              @click="handleDelete(row)"
            />
          </el-tooltip>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty class="vab-data-empty" description="暂无数据" />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit
      ref="editRef"
      :account-operate-list="accountOperateList"
      :platform-id="platformId"
      @fetch-data="fetchData"
    />
  </div>
</template>

<script>
  // import { getMeAssetInfoList } from '@/api/assetManagement'
  import { Delete, Plus, Search, Edit } from '@element-plus/icons-vue'
  // import { details, doDelete } from '@/api/userManagement'
  import {
    deleteTopicRecord,
    fetchTopicRecord,
    fetchTopicRecordList,
    // flexibleTopicRecord,
    // editTopicRecord,
  } from '@/api/topics'
  import { fetchaccountOperate } from '@/api/accountManagement'
  export default defineComponent({
    name: 'MyAssetList',
    components: {
      Edit: defineAsyncComponent(() => import('./components/Edit')),
    },
    setup() {
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')
      // const router = useRouter()
      const state = reactive({
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          name: '',
          status: '',
          pageNum: 1,
          pageSize: 10,
        },
        statusList: [
          {
            label: '闲置中',
            value: 0,
          },
          {
            label: '使用中',
            value: 1,
          },
          {
            label: '报废',
            value: 2,
          },
        ],
        // platformId: null,
        // accountId: null,
        accountOperateList: [],
      })
      // const route = useRoute()

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleDelete = (row) => {
        if (row.id) {
          try {
            $baseConfirm('你确定要删除当前项吗', null, async () => {
              const { msg } = await deleteTopicRecord({ ids: row.id })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } catch (e) {
            console.log(e)
          }
        } else {
          if (state.selectRows.length > 0) {
            try {
              const ids = state.selectRows.map((item) => item.id).join()
              $baseConfirm('你确定要删除选中项吗', null, async () => {
                const { msg } = await deleteTopicRecord({ ids })
                $baseMessage(msg, 'success', 'vab-hey-message-success')
                await fetchData()
              })
            } catch (e) {
              console.log(e)
            }
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      // const handleDetail = (row) => {
      //   router.push(`/asset/assetManagementDetail?id=${row.id}`)
      // }
      const handleEdit = async (row) => {
        try {
          if (row.id) {
            console.log(row.id, 'ssasas')
            const { data: data } = await fetchTopicRecord({ assetId: row.id })
            state['editRef'].showEdit(data)
          } else {
            state['editRef'].showEdit()
          }
        } catch (e) {
          console.log(e)
        }
      }

      const fetchAccountOperate = async () => {
        try {
          state.listLoading = true
          // state.queryForm.platformId = state.platformId
          // state.queryForm.accountId = state.accountId
          const { data } = await fetchaccountOperate()
          state.accountOperateList = data
          state.listLoading = false
        } catch (e) {
          console.log(e)
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNum = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNum = 1
        fetchData()
      }
      const fetchData = async () => {
        try {
          state.listLoading = true
          console.log(1, state.queryForm)
          if (state.queryForm.id) {
            let queryForm = {}
            queryForm = {
              platformId: state.queryForm.id,
              pageNum: state.queryForm.pageNum,
              pageSize: state.queryForm.pageSize,
            }
            const { data } = await fetchTopicRecordList(queryForm)
            state.list = data.list
            state.total = data.total
            state.listLoading = false
            return
          }
          const { data } = await fetchTopicRecordList(state.queryForm)
          state.list = data.list
          state.total = data.total
          state.listLoading = false
        } catch (e) {
          console.log(e)
        }
      }
      onMounted(() => {
        fetchData()
        fetchAccountOperate()
      })

      return {
        ...toRefs(state),
        setSelectRows,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        // handleDetail,
        handleEdit,
        handleDelete,
        Delete,
        Plus,
        Search,
        Edit,
      }
    },
  })
</script>

<style scoped>
  .toolbar {
    margin-bottom: 4px;
  }

  :deep() {
    .cell .el-button {
      width: 32px;
    }
  }
</style>
